﻿Chenxin.UI.WorkReportPage = Chenxin.extend(Chenxin.UI.AddPageCommon, {
    actionUrl: "",
    createForm: function() {
        return new Chenxin.UI.WorkReportForm({
            scope: this
        });
    },
    customerInit: function() {
        this.editor = $('#txtrContent').xheditor();
    },
    setReceiver: function(jsonVal) {
        var strLsRecId = $("#hidReceiverId").val();
        var strLsRecNm = $("#txtReceiverName").val();
        var strLsId = "";
        var strLsNm = "";
        var lsNm;
        for (var idxCnt = 0; idxCnt < jsonVal.length; idxCnt++) {
            if (strLsRecId.indexOf(jsonVal[idxCnt].id) == -1) {
                strLsId = strLsId + "," + jsonVal[idxCnt].id;
                lsNm = jsonVal[idxCnt].name.split("/");
                strLsNm = strLsNm + "," + lsNm[lsNm.length - 1];
                //strLsNm = strLsNm + "," + jsonVal[idxCnt].name;
                if ((jsonVal[idxCnt].id).indexOf("Role_") != -1) {
                    strLsNm = strLsNm + "(角色)";
                }
            }
        }
        if (strLsRecId != "") {
            strLsId = strLsRecId + strLsId;
            strLsNm = strLsRecNm + strLsNm;
        } else {
            if (strLsId != "") {
                strLsId = strLsId.substring(1);
                strLsNm = strLsNm.substring(1);
            }
        }
        $("#hidReceiverId").val(strLsId);
        $("#txtReceiverName").val(strLsNm);
    },
    initEvent: function() {
        var objPage = this;
        //新规
        if (objPage.form.currentId == undefined) {
            /****************事件：组织用户选择**********/
            $("#aOrgUsers").bind("click", function() {
                if (objPage.OrgTree == undefined) {
                    objPage.OrgTree = new Chenxin.UI.OrgTree();
                } else {
                    objPage.OrgTree.reload();
                }
                //$("#divOrgUsers").show();
                $("#divOrgUsers").slideToggle("fast");
            })
            /****************事件：组织用户确定**********/
            $("#aFixOrg").click(function() {
                var jsonVal = objPage.OrgTree.getSelectedValNm();
                objPage.setReceiver(jsonVal);
                //$("#divOrgUsers").hide();
                $("#divOrgUsers").slideToggle("fast");
            });
            /****************事件：组织用户关闭**********/
            $("#aCloseOrg").click(function() {
                $("#divOrgUsers").slideToggle("fast");
            });
            /****************事件：组织用户选择**********/
            $("#aRoleUsers").click(function() {
                if (objPage.RoleTree == undefined) {
                    objPage.RoleTree = new Chenxin.UI.RoleTree();
                } else {
                    objPage.RoleTree.reload();
                }
                //$("#divRoleUsers").show();
                $("#divRoleUsers").slideToggle("fast");
            })
            /****************事件：组织用户确定**********/
            $("#aFixRole").click(function() {
                var jsonVal = objPage.RoleTree.getSelectedValNm();
                objPage.setReceiver(jsonVal);
                $("#divRoleUsers").hide();
            });
            /****************事件：组织用户关闭**********/
            $("#aCloseRole").click(function() {
                $("#divRoleUsers").slideToggle("fast");
            });
            /****************事件：清除用户列表**********/
            $("#aClearUsers").click(function() {
                $("#hidReceiverId").val("");
                $("#txtReceiverName").val("");
            });
        }

        /**************事件：返回一览*************/
        $("#aRet").click(function() {
            window.location.href = "WorkReportSearch.aspx";
        });

        /**************事件：删除文件*************/
        $("#chkFile").click(function() {
            if ($(this).attr("checked") == false) {
                if (confirm("确定删除附件吗？")) {

                } else {
                    $(this).attr("checked", true);
                }
            } else {
                if (objPage.form.fileNames.length > 0) {
                    alert("请选择一个附件进行删除！");
                    $(this).attr("checked", false);
                }
            }
        });

        var isComplete = true;
        var hasFile = false;
        /****************事件：发送工作报告**********/
        $("#aSendReport").click(function() {

            var model = objPage.form.getParam();

            if (objPage.form.manualPreCheck != undefined) {
                if (objPage.form.manualPreCheck() == false) {
                    return;
                }
            }
            $("#thumbnialUploadify").uploadifyUpload();
            if (isComplete && !hasFile) {
                objPage.form.save();
                objPage.form.fileNames.length = 0;
                objPage.form.saveNames.length = 0;
            }
        });

        //上传附件Flash
        $("#thumbnialUploadify").uploadify({
            uploader: '/js/uploadify.swf',
            script: '/service/workReportSend.ashx?op=fileupload',
            cancelImg: '/images/cancel.png',
            buttonImg: '/images/fileupload.png',
            auto: false,
            multi: false,
            height: 24,
            width: 190,
            sizeLimit: 1048576,
            onSelect: function(event, queueID, fileObj) {

                $("#chkFile").attr("checked", false);
                hasFile = true;
            },
            onSelectCancel: function() {
                if( !hasFile ){
                    isComplete = true;
                }    
            },
            onClick: function() {
                if( !hasFile ){
                    isComplete = false;
                }
            },
            onComplete: function(event, queueID, fileObj, response, data) {
                if (response) {
                    try {
                        var json = eval("(" + response + ")");
                        if (json.success == 1) {
                            if (data.fileCount == 0) {
                                objPage.form.fileNames.push(json.data.filename);
                                objPage.form.saveNames.push(json.data.savename);
                                objPage.form.save();
                                objPage.form.fileNames.length = 0;
                                objPage.form.saveNames.length = 0;
                                isComplete = true;
                                hasFile = false;
                            }
                        } else if (json.success == 0) {
                            alert(json.msg);
                        } else {
                            alert(json.msg);
                            window.location.href = Chenxin.loginUrl;
                        }
                    } catch (e) {
                        alert('返回数据格式错误，文件上传失败！');
                    }
                }
            },
            onError: function(event, queueID, fileObj) {
                alert("文件(" + fileObj.name + ")上传失败");
            },
            onCancel: function(event, queueID, fileObj) {
                isComplete = true;
                hasFile = false;
            }
        });

        /****************事件：删除工作报告**********/
        $("#divBtDel").click(function() {
            objPage.form.op = "del";
            objPage.form.del();
        });
    }
});

Chenxin.UI.WorkReportForm = Chenxin.extend(Chenxin.UI.AddFormCommon,{
    id:"divForm",
    actionUrl:'/service/workReportSend.ashx',
    op:"save",
    uploadFile:true,
    afterGetFormJson:function(json){
        json.Content = escape(this.editor.getSource());
        if(json.chkFile != undefined){
            delete json.chkFile;
        }
        if($("#chkFile").attr("checked") == false){
            this.form.additionalParam.uploadfl = $("#chkFile").val();
        }
    },
    manualReset:function(){
        this.scope.editor.setSource('');
    },
    loadDefVal:function(){
        if($("#ctl00_main_hidOutBox").val() != ""){
            var model = eval("(" + $("#ctl00_main_hidOutBox").val() + ")");
            Chenxin.Fn.setFormValue(this.id,this.comboList,model);
            this.currentId = model.RecordId;
            $("#divBtDel").show();
            $("#divBtRet").show();
            $("#chkFile").attr("checked",true);
            $("#aTitle").text("编辑工作报告");
            $("#h2Title").text("编辑工作报告");
        }else if($("#ctl00_main_hidRelatedOutBox").val() != ""){
            var model = eval("(" + $("#ctl00_main_hidRelatedOutBox").val() + ")");
            delete model.RecordId;
            delete model.ReceiverName;
            delete model.ReceiverId;
            Chenxin.Fn.setFormValue(this.id,this.comboList,model);
            $("#aTitle").text("转发工作报告");
            $("#h2Title").text("转发工作报告");
        }
    },
    saveSucc:function(){
        if($("#hidRecordId").val() != ""){
            window.location.href = "WorkReportSearch.aspx";
        }
    },
    delSucc:function(){
        window.location.href = "WorkReportSearch.aspx";
    },
    manualPreCheck:function(){
        if($("#txtReceiverName").val() == ""){
            alert("请选择收件对象");
            return false;
        }
        if($("#txtSubject").val() == ""){
            $("#txtSubject").focus()
            alert("请输入报告主题");
            return false;
        }
        return true;
    }
});

/**
*@description 组织用户树
*/
Chenxin.UI.OrgTree = Chenxin.extend(Chenxin.UI.CheckBoxTree,{
    actionUrl:"/service/workReportSend.ashx",
    params:{op:"getOrgTree"},
    renderTo:"divOrgTree"
});

/**
*@description 角色用户树
*/
Chenxin.UI.RoleTree = Chenxin.extend(Chenxin.UI.CheckBoxTree,{
    actionUrl:"/service/workReportSend.ashx",
    params:{op:"getRoleTree"},
    renderTo:"divRoleTree"
});

$(document).ready(function(){
   var page = new Chenxin.UI.WorkReportPage();
});

